package com.baidu.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baidu.constant.MessageConstant;
import com.baidu.constant.RedisConstant;
import com.baidu.entity.PageResult;
import com.baidu.entity.QueryPageBean;
import com.baidu.entity.Result;
import com.baidu.pojo.CheckGroup;
import com.baidu.pojo.Setmeal;
import com.baidu.service.CheckGroupService;
import com.baidu.service.SetMealService;
import com.baidu.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;
import java.io.IOException;
import java.util.*;
/**
 * 和套餐相关操作
 */

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

	@Reference
	private SetMealService setMealService;
	@Reference
	private CheckGroupService checkGroupService;

@Autowired
private JedisPool jedisPool;

	//上传图片组件
@RequestMapping("/upload")
//本地图片上传到controller,controller上传到七牛云。
public Result upload(@RequestParam("imgFile") MultipartFile imgFile ){//所有的文件都用这个参数，包裹excel表
	//先获得原始文件名的后缀名。
	String originalFilename = imgFile.getOriginalFilename();
	int index = originalFilename.lastIndexOf(".");
	String extention = originalFilename.substring(index);//.jpg


	String fileName = UUID.randomUUID().toString()+extention;

	try {
		QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
		//把每个添加到七牛云的图片，也添加到缓存中
	jedisPool.getResource().sadd(RedisConstant.CACHE_PICTURE,fileName);


		return  new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);

	} catch (IOException e) {
		e.printStackTrace();
		return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
	}


}
/**
 * 跟据套餐基本信息和检查组id新增套餐
 * @param checkgroupIds 检查组id数组
 * @param setmeal 套餐基本信息
 * @return Result
 */

@PreAuthorize("hasAuthority('SETMEAL_ADD')")
	@RequestMapping("/add")
	//这里的传递过来的检查项id不是json格式的，可以不用加@RequestBody注解
	public Result addSetMeal(Integer[] checkgroupIds, @RequestBody Setmeal setmeal){

		try {
			setMealService.addSetMeal(checkgroupIds,setmeal);
			return  new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
		} catch (Exception e) {
			return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
		}


	}


	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
		PageResult pageResult = setMealService.pageQuery(queryPageBean);
		//System.out.println(pageResult.getRows()+"//"+pageResult.getTotal());
		return pageResult;

	}

	/**
	 * 跟据套餐id回显该套餐的基本信息
	 * @param id 套餐id
	 * @return Result
	 */
	@RequestMapping("/findSetmealById")
	public Result findSetmealById(Integer id){
		try {
			Setmeal setmeal = setMealService.findByIdOnlySetmeal(id);
			return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
		}
	}

	/**
	 * 查询所有检查组信息展示在编辑页面
	 * @return
	 */
	@RequestMapping("/findAllChenkgroups")
	public Result findAllChenkgroups(){
		try {
			List<CheckGroup> checkGroupList = checkGroupService.findAll();
			return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
		}
	}

	/**
	 * 根据指定的套餐查询查询其所拥有的检查组id
	 * @param id 套餐id
	 * @return
	 */

	@RequestMapping("/findChenckgroupIdBySetmealId")
	public Result findChenckgroupIdBySetmealId(Integer id){
		try {
			List<Integer> list = setMealService.showCgidBySid(id);
			return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
		} catch (Exception e) {
			return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
		}
	}

	/**
	 * 根据指定id删除套餐
	 * @param id
	 * @return
	 */

	@RequestMapping("/deleteSetmeal")
	public Result deleteSetmeal(Integer id) {

		Result result = setMealService.delete(id);
		return result;
	}

	/**
	 * 通过套餐基础信息和检查组id数组修改套餐信息
	 * @param setmeal
	 * @param checkgroupIds
	 * @return
	 */
	@RequestMapping("/updateStemealById")
	public Result updateStemealById(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){

			Result result = setMealService.edit(setmeal,checkgroupIds);
			return result;

	}

}
